﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoardController.Helper
{
    public class Manifest
    {
        public static string WarningMsg = "";

        public static string[] cameraIds = new[] { "944622071744", "918512070748", "944622073161", "943222070816" };
        public static int RidarCount = 4;

        public static int cameraRow = 480; // 720
        public static int cameraCol = 640; // 1280
        public static double cameraViewHeight = 1;
        public static double cameraViewWidth = 1.6;

        public static double cameraMaxX = cameraViewWidth / 2;
        public static double cameraMinX = -1 * cameraViewWidth / 2;
        public static double cameraMaxY = cameraViewHeight / 2;
        public static double cameraMinY = -1 * cameraViewHeight / 2;
        public static double cameraMaxZ = 3;
        public static double cameraMinZ = 1.3;

        public static double cellSize = 0.05;

        public static double frameHeight = cameraViewHeight;
        public static double frameWidth = cameraViewWidth * cameraIds.Length;

        public static double SlideLength = 7.5 + 0.5;
        public static double PrintFrameHeight = 3; //这个值应该比所有的喷涂范围都要高一点。最重要的，要比FaceUpHeightMax + FaceUpMargin大

        public static double PrintAreaMin = 0.3;
        public static double PrintAreaMax = 0.5;

        // 加料参数
        public static double FeedingUpThreshold = 5; // 单位kg 
        public static double FeedingDownThreshold = 0.05; // 单位kg 
        public static double CoatingRate = 1000; // 涂布率
        public static int FeedMaxMillSecond = 25000; // 最大加料时间
        public static double FeedingVoltageOffset = 0; // 0.213 单位v
        public static double FeedingRatio = 1.7; // kg/v 系数
        public static double FeedingSlidPos = 4.946; // 1.92

        public static int MovedThreshold = 100;
        public static int TargetThreshold = 200;





        public static bool Emergency = false;

        public static double sumA = 0;
        public static double sumB = 0;

        #region  ArmParameters
        //相对于吊篮下沿
        public static double CameraInstalledSize = 1.8;
        public static double ArmInstalledSize = 1.3;

        public static double PrintSpaceXOffset = -0.3; //  -1.05 -0.5 -4.1;  // 喷的偏左，说明读地图偏右，应该减小
        public static double PrintSize = 0.3;   //这个地方应该改成plainheight


        public static double FacePlainMax = 1.35;// 1.1
        public static double FacePlainMin = FacePlainMax - PrintSize;   //1
        public static double FacePlainMax2 = FacePlainMax - PrintSize;   //1
        public static double FacePlainMin2 = FacePlainMax2 - PrintSize;  //0.7

        public static double FacePlainMargin = 0.4;

        public static double WorkToViewOffset = FacePlainMax + ArmInstalledSize - (CameraInstalledSize - cameraViewHeight / 2);

        //0.83- 0.33
        public static double FaceUpMax = 1.3;
        public static double FaceUpMin = 0.7;


        //0.62-0.12 
        public static double FaceDownMax = 0.4;
        public static double FaceDownMin = -0.2;


        //0.93-0.33
        public static double FaceLeftRightHeightMax = 1.1;
        public static double FaceLeftRightHeightMin = 0.5;

        public static double FaceLeftMargin = 0.8;
        public static double FaceRightMargin = -1;
        #endregion

        public static DataFilter SpeedLeaveWall = new DataFilter(1);
        public static DataFilter YawSpeedByDepth = new DataFilter(1);
    }

    public class DataFilter
    {
        int Size = 0;
        List<double> DataList = new List<double>();
        double[] DataArray;
        int index = 0;
        public DataFilter(int size)
        {
            Size = size;
            DataArray = new double[Size];
        }

        public void Insert(double data)
        {
            //if (DataList.Count > Size)
            //    DataList.RemoveAt(0);
            //DataList.Add(data);
            DataArray[index] = data;
            index = (index + 1) % Size;
        }

        public double Average()
        {
            //if (DataList.Count > 0)
            //    return DataList.Average();
            //else
            //    return 0;
            return DataArray.Average();
        }

        //********************test********************//

        public void DebugPrint()
        {
            //string str = "";
            //for (int i = 0; i < DataArray.Length; i++)
            //{
            //    str += DataArray[i].ToString() + " ";
            //}
            //Debug.Print(str);
            System.Diagnostics.Debug.Print(DataArray[(index + Size - 1) % Size].ToString());
        }

        public double GetCurrentValue()
        {
            return DataArray[(index + Size - 1) % Size];
        }

        //********************end*********************//
        public double CircleAverage(double baseline)
        {
            double[] tempArray = new double[Size];

            for (int i = 0; i < Size; i++)
            {
                if (DataArray[i] - baseline > 180)
                {
                    tempArray[i] = (DataArray[i] - baseline) - 360;
                }
                else if (DataArray[i] - baseline < -180)
                {
                    tempArray[i] = 360 + (DataArray[i] - baseline);
                }
                else
                {
                    tempArray[i] = DataArray[i] - baseline;
                }

            }

            return tempArray.Average();
        }

        public double WeightedAverage()
        {
            return 0;
        }
    }
}
